const mysql = require('../../utils/db');
const BaseModel = require('../BaseModel');
const _table_name = "music_lyric";

exports.findAll = (success, fail) => {
    let order = " ORDER BY lyric_id DESC";
    let sqlStr = `
                    SELECT
                        lrc.*, music_name
                    FROM
                        ${_table_name} lrc
                    LEFT JOIN music muc ON (
                        lrc.lyric_id = muc.lyric_id
                    )
                    WHERE
                        lrc.lyric_id != '0'
                    ${order}
                    `;

    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

exports.updateLyric = async (params, updatekey, success, fail) => {
    let sqlStr = BaseModel.update(_table_name, params, updatekey);

    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

exports.findLyric = async (params) => {
    this.data = null;
    this.err = null;
    let sqlStr = BaseModel.find(_table_name, params);
    
    await mysql.row(sqlStr)
        .then(data => {
            this.data = data;
        })
        .catch(err => {
            this.err = err;
        });

    this.success = (fn) => {
        if (this.data)
            return fn(this.data);
    }
    this.fail = (fn) => {
        if (this.err)
            return fn(this.err);
    }
    return this;
}

exports.delLyric = (params, success, fail) => {
    let sqlStr = BaseModel.delete(_table_name, params);
    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

exports.addLyric = (params, success, fail) => {
    let sqlStr = BaseModel.add(_table_name, params);
    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}